package org.weasel.controllers;

import org.eclipse.swt.widgets.Composite;
import org.weasel.business.Task;
import org.weasel.business.UserStory;
import org.weasel.models.TaskList;
import org.weasel.views.TaskListComposite;

public class TaskListController {
	private UserStoryViewController parentController;
	private TaskListComposite taskListComposite;
	private TaskList taskList; //contains the tasks of the user-story
	
	/**
	 * Not necessary to create singleton
	 * @param parentController
	 */
	public TaskListController(UserStoryViewController parentController){
		this.parentController = parentController;
		this.taskList = new TaskList(); //create model object
	}
	
	public TaskListComposite createTaskListComposite(Composite parent, int style){
		UserStory story = parentController.getUserStory();
		this.taskList.init(story.getTasks());
		taskListComposite = new TaskListComposite(parent,style, this);
		try {
			taskListComposite.getViewer().setInput(this.taskList);
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		taskListComposite.setController(this);
		return taskListComposite;
	}
	
	public void refreshComposite(){
		this.taskList = new TaskList();
		this.taskList.init(parentController.getUserStory().getTasks());
		taskListComposite.getViewer().setInput(this.taskList);
	}

	public TaskList getTaskList() {
		return taskList;
	}

	public void addTask() {
		taskList.addTask();
		ApplicationController.instance().changesOccured();
	}

	public void removeTask(Task task) {
		taskList.removeTask(task);
		ApplicationController.instance().changesOccured();
	}

	public void taskChanged(Task task) {
		taskList.taskChanged(task);
		ApplicationController.instance().changesOccured();
	}
	
}
